package com.cmsz.centralservice.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期、时间类
 */
public class DateUtil {

    /**
     * 日期转换
     *
     * @param time 时间戳
     * @param fmt  时间格式:yyyy-MM-dd HH:mm:ss
     * @return 指定时间字符串表达式
     */
    public static String formatTime(Timestamp time, String fmt) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(fmt);
        LocalDateTime localDateTime = time.toLocalDateTime();
        return dateTimeFormatter.format(localDateTime);
    }

    /**
     * 格式化日期
     *
     * @param date 日期
     * @param fmt  时间格式:yyyy-MM-dd HH:mm:ss
     * @return 指定时间字符串表达式
     */
    public static String formatDate(Date date, String fmt) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(fmt);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return dateTimeFormatter.format(localDateTime);
    }

    /**
     * 增加天数
     */
    public static Timestamp addDay(Timestamp time, Integer day) {
        LocalDateTime localDateTime1 = LocalDateTime.ofInstant(time.toInstant(), ZoneId.systemDefault());
        return Timestamp.valueOf(localDateTime1.plusDays(day));
    }

    /**
     * 获取当前月份，格式yyyyMM
     */
    public static String getMonth() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM");
        return localDateTime.format(dateTimeFormatter);
    }

    /**
     * 获取t天前日期，格式yyyyMM
     */
    public static String getDateyyyyMMdd(int t) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime localDateTime = LocalDateTime.now();
        localDateTime = localDateTime.plusDays(-t);

        return localDateTime.format(dateTimeFormatter);
    }

    public static String getDateyyyyMMdd() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.format(dateTimeFormatter);
    }

    public static String getDateyyyyMMddHHmm() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
        return dateTimeFormatter.format(localDateTime);
    }

    public static String getDateyyyyMMddHHmmss() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.format(dateTimeFormatter);
    }

    public static Date getDateyyyyMMddHHmmssSSS() throws ParseException {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        LocalDateTime localDateTime = LocalDateTime.now();
        String landing = localDateTime.format(dateTimeFormatter);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * 取当前时间，精确到秒， 格式为 yyyy-MM-dd HH:mm:ss
     */
    public static String getCurrentDateTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return localDateTime.format(dateTimeFormatter);
    }

    /**
     * 取当前时间精确到毫秒 ，格式为 yyyy-MM-dd HH:mm:ss.SSS
     */
    public static String getCurrentDateTimeMS() {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        return localDateTime.format(dateTimeFormatter);
    }

    /**
     * 根据所需要的时间格式，获取系统当前时间的字符串表示形式
     *
     * @param pattern 时间格式
     * @return 当前系统时间的字符串表示形式
     */
    public static String getCurrentTimeDescByPattern(String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime arriveDate = LocalDateTime.now();
        return arriveDate.format(dateTimeFormatter);

    }

    /**
     * 对当前日期字符串增加天数(当为负数时变减少天数)
     *
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @param day     增加天数(当为负数时变减少天数)
     * @return 增加天数后的日期字符串
     */
    public static String addDay(String strDate, String pattern, Integer day) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = format.parse(strDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return format.format(cal.getTime());
    }

    /**
     * 对当前日期字符串增加月份(当为负数时变减少月份)
     *
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @param month   增加月份(当为负数时变减少月份)
     * @return 增加月份后的日期字符串
     */
    public static String addMonth(String strDate, String pattern, Integer month) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = format.parse(strDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return format.format(cal.getTime());
    }

}
